package org.ysu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.ysu.entity.Admin;
import org.ysu.entity.Clue;
import org.ysu.entity.Role;
import org.ysu.mapper.ClueMapper;
import org.ysu.service.ClueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.ysu.vo.ResultVO;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 线索表 服务实现类
 * </p>
 *
 * @author CaoYuJia
 * @since 2024-08-20
 */
@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue> implements ClueService {

    @Resource
    ClueMapper clueMapper;
    @Override
    public IPage<Clue> list(Integer pageNo, Integer pageSize, Long rId) {
        // 创建分页对象
        Page<Clue> page = new Page<>(pageNo, pageSize);

        // 构建查询条件
        QueryWrapper<Clue> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("r_id", rId.toString()); // 以 rId 开头的匹配

        // 执行分页查询
        IPage<Clue> cluePage = this.page(page, queryWrapper);

        return cluePage;
    }
    @Override
    public boolean add(Long ccId, Long fId, String province, String city, String county, String township, String village, Boolean riskMan, Boolean riskSubstance, Boolean riskNature, Boolean riskFinance, Boolean riskSocial, Integer riskLevel, String riskRemark, Boolean verify,Long rId) {
        return this.save(new Clue(ccId,fId,province,city,county,township,village,riskMan,riskSubstance,riskNature,riskFinance,riskSocial,riskLevel,riskRemark,verify,rId));
    }

    @Override
    public boolean update(Long id, Long ccId, Long fId, String province, String city, String county, String township, String village, Boolean riskMan, Boolean riskSubstance, Boolean riskNature, Boolean riskFinance, Boolean riskSocial, Integer riskLevel, String riskRemark, Boolean verify,Long rId) {
        return this.updateById(new Clue(id,ccId,fId,province,city,county,township,village,riskMan,riskSubstance,riskNature,riskFinance,riskSocial,riskLevel,riskRemark,verify,rId));
    }

    @Override
    public boolean del(Long id, Boolean valid) {
        return this.updateById(new Clue(id, valid));
    }

    @Override
    public boolean verify(Long id, Boolean verify,Boolean valid) {
        return this.updateById(new Clue(id, verify,valid));
    }


    @Override
    public boolean check(Long id, Long fId) {
        QueryWrapper<Clue> wrapper=new QueryWrapper<>();
        wrapper.eq("f_id",fId).eq("valid",true);
        if(null!=id){
            wrapper.ne("id",id);
        }
        return this.count(wrapper)==0;
    }

    @Override
    public List<String> getNameByClueId(Long clueId) {
        return clueMapper.getNameByClueId(clueId);
    }

    @Override
    public IPage<Clue> recordList(Integer pageNo, Integer pageSize, Long verifierId) {
        IPage<Clue> cluePage = this.page(
                new Page<>(pageNo, pageSize),
                new QueryWrapper<Clue>()
                        .inSql("id", "SELECT clue_id FROM admin_clue WHERE admin_id = " + verifierId)
        );
        for (Clue clue : cluePage.getRecords()) {
            List<String> names = this.getNameByClueId(clue.getId());
            String result = names.isEmpty() ? "无" : String.join(", ", names);
            clue.setVName(result);
        }
        return cluePage;
    }
}
